<?php
class AccountTokenMultiPeriodStat extends CFormModel {

	public $tokenDateStart;
	public $tokenDateEnd;
	public $projectId;

	public function rules() {
		return array(
			array(
				'tokenDateStart',
				'required'
			),
			array(
				'tokenDateStart, tokenDateEnd',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'projectId',
				'numerical',
				'integerOnly' => true,
			),
		);
	}

	public function attributeLabels() {
		return array(
			'projectId' => '项目',
			'tokenDateStart' => '时间',
		);
	}

	/**
	 * @return CDbCriteria
	 */
	public function createCriteria() {
		$criteria = new CDbCriteria;

		if ($this->tokenDateStart && $this->tokenDateEnd) {
			$criteria->addBetweenCondition('tokenDate', $this->tokenDateStart, $this->tokenDateEnd);
		} else {
			if ($this->tokenDateStart) {
				$criteria->compare('tokenDate', '>=' . $this->tokenDateStart);
			}

			if ($this->tokenDateEnd) {
				$criteria->compare('tokenDate', '<=' . $this->tokenDateEnd);
			}
		}
		if ($this->projectId) {
			$criteria->compare('projectId', $this->projectId);
		} else {
			$criteria->addInCondition('projectId', AccountTokenProject::getCompanyProjectIds());
		}
		return $criteria;
	}
}
